// ActionScript file
import common.components.DividerBox;
import common.components.DynamicVBox;
import common.utils.AuthorityInvoker;

import flash.events.MouseEvent;

import flexlib.controls.SuperTabBar;
import flexlib.controls.tabBarClasses.SuperTab;
import flexlib.events.TabReorderEvent;

import mx.collections.ArrayCollection;
import mx.containers.Canvas;
import mx.containers.Panel;
import mx.containers.VBox;
import mx.controls.Alert;
import mx.controls.Label;
import mx.controls.SWFLoader;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.modules.ModuleLoader;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Bindable]private var treeData:XMLList;
[Bindable]public var iIdx:int = -1;

private function initData():void
{
	//init web service
	var wsdl:String = this.parentApplication.AppServer + "LoginService.asmx?wsdl";
	LoginSvs.loadWSDL(wsdl);
	//初始化开始菜单
	MenuSvs.send();
	
	//添加Home tab
	var vBox:VBox = new VBox();
	vBox.label = "主页";
	//vBox.icon = lock_icon;
	nav.addChild(vBox);
	callLater(initNonClosableTab);
}

private function initNonClosableTab():void {
	nav.setClosePolicyForTab(0, SuperTab.CLOSE_NEVER);
}

private function onGetConfigResult(event:ResultEvent):void
{
	var xmlObj:XML = event.result as XML;
	//this.treeData = xmlObj.elements();
}

private function onFault(event:FaultEvent):void
{
	Alert.show(event.fault.message);
}

private function logout():void{
	LoginSvs.Logout(this.parentApplication.SessionID);
	var event:Event = new Event("logout");
	dispatchEvent(event);
}

private function menu_Click(event:MouseEvent):void{
	var menuObj:Object = event.currentTarget.getRepeaterItem();
	loadComponent(menuObj.@url, menuObj.@name);
}

private function loadComponent(url:String, label:String):void{
	if (null == url || url == '')
		return;
	
	for each (var childTab:VBox in nav.getChildren()){
		//只是通过标签名字判断.如果一样就直接打开标签.
		if(childTab.label == label){
			nav.selectedIndex = nav.getChildIndex(childTab);
			//跳转到指定标签.
			return;
		}
	}
	/*
	var vBox:DynamicVBox = new DynamicVBox();
	vBox.label = label;
	vBox.url = url;
	*/
	
	var vBox:VBox = new VBox();
	//vBox.width = this.parent.width;
	//vBox.height = this.parent.height;
	vBox.label = label;
	nav.addChild(vBox);
	var mdLoader:ModuleLoader = new ModuleLoader();
	mdLoader.width = nav.width - 4;
	mdLoader.height = nav.height - 42;
	vBox.addChild(mdLoader);
	mdLoader.url = url;
	mdLoader.loadModule();
	nav.selectedChild = vBox;	
}

private var myColorTransform:ColorTransform;
private function onImageMouseOver(event:MouseEvent):void
{
	myColorTransform = new ColorTransform(1,1,1,1,54,54,54);
	//imgLogout.transform.colorTransform = myColorTransform;
	event.target.transform.colorTransform = myColorTransform;
	
}
private function onImageMouseOut(event:MouseEvent):void
{
	myColorTransform = new ColorTransform();
	event.target.transform.colorTransform = myColorTransform;
}
/*
private function tree_itemClick(event:ListEvent):void {
	var item:Object = Tree(event.currentTarget).selectedItem;
	if (treeMenu.dataDescriptor.isBranch(item)) {
		treeMenu.expandItem(item, !treeMenu.isItemOpen(item), true);
	}
	else{
		var selectedNode:XML = Tree(event.target).selectedItem as XML;
		var url:String = selectedNode.@url;
		var label:String = selectedNode.@label;
		if (null == url || url == '')
			return;
		
		for each (var childTab:VBox in nav.getChildren()){
			//只是通过标签名字判断.如果一样就直接打开标签.
			if(childTab.label == label){
				nav.selectedIndex = nav.getChildIndex(childTab);
				//跳转到指定标签.
				return;
			}
		}
		
		//一个新的标签，打开新Tab页
		
		//var swfLoader:SWFLoader = new SWFLoader();
		//var vBox:VBox = new VBox();
		//vBox.label = label;
		//vBox.setStyle("closable", true);
		//swfLoader.source = url;
		//vBox.addChild(swfLoader);
		
		var vBox:DynamicVBox = new DynamicVBox();
		vBox.label = label;
		vBox.url = url;
		
		nav.addChild(vBox);
		nav.selectedChild = vBox;
	}
}
*/